package br.com.advance.automacao.web.administrativo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import br.com.advance.automacao.model.CLP;
import br.com.advance.automacao.model.Erro;
import br.com.advance.automacao.services.CLPService;
import br.com.advance.automacao.services.ErroService;
import br.com.advance.automacao.web.utils.BaseBean;

@ManagedBean
@ViewScoped
public class CadastroErroMBean extends BaseBean {

	/**
	 *
	 */
	private static final long serialVersionUID = 4180491670576658298L;

	private static final Logger logger = Logger.getLogger(CadastroErroMBean.class);

	private List<Erro> listErro = new ArrayList<Erro>();
	private List<SelectItem> listCLPSelectItem = new ArrayList<SelectItem>();

	private Long idCLPSelecionado;

	@Autowired
	private ErroService erroService;

	@Autowired
	private CLPService clpService;

	public void initialize() {
		logger.info("initialize...");
		List<CLP> listCLP = clpService.buscarTodosAtivos();
		for(CLP clp : listCLP) {
			listCLPSelectItem.add(new SelectItem(clp.getId(), clp.getNome()));
		}
	}

	protected boolean verificaUsuarioLogado() {
		return true;
	}


	public void consultar() {
		if(idCLPSelecionado == null) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Nenhum Equipamento Selecionado.", null);
			FacesContext.getCurrentInstance().addMessage(null, message);
			listErro.clear();
			return;
		}
		listErro = erroService.buscarErroPorClp(idCLPSelecionado);
	}

	public void abreEdicaoErro(Erro item) {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", false);
		options.put("modal", true);
		options.put("closeable", false);
		options.put("includeViewParams", true);

		Map<String, List<String>> params = new HashMap<String, List<String>>();
		List<String> values = new ArrayList<String>();
		values.add(String.valueOf(item.getId()));
		params.put("idErro", values);

		RequestContext.getCurrentInstance().openDialog("editarErro", options, params);
	}

	public void abreInclusaoErro() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", false);
		options.put("modal", true);
		options.put("closeable", false);
		options.put("includeViewParams", true);

		Map<String, List<String>> params = new HashMap<String, List<String>>();
		List<String> values = new ArrayList<String>();
		values.add(String.valueOf(idCLPSelecionado));
		params.put("idCLP", values);

		RequestContext.getCurrentInstance().openDialog("editarErro", options, params);
	}

	public void abreImportarXLS() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", false);
		options.put("modal", true);
		options.put("closeable", false);
		options.put("includeViewParams", true);

		Map<String, List<String>> params = new HashMap<String, List<String>>();
		List<String> values = new ArrayList<String>();
		values.add(String.valueOf(idCLPSelecionado));
		params.put("idCLP", values);

		RequestContext.getCurrentInstance().openDialog("importarXLSErros", options, params);
	}

	public void concluiEdicaoErro(SelectEvent event) {
		listErro = erroService.buscarErroPorClp(idCLPSelecionado);

		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operação realizada com sucesso.", null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void concluiImportacaoXLS(SelectEvent event) {
		listErro = erroService.buscarErroPorClp(idCLPSelecionado);

		@SuppressWarnings("unchecked")
		List<String> erros = (List<String>) event.getObject();

		if(erros.isEmpty()) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operação realizada com sucesso.", null);
			FacesContext.getCurrentInstance().addMessage(null, message);
		} else {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Operação realizada com sucesso. Algumas linhas não foram processadas:", null);
			FacesContext.getCurrentInstance().addMessage(null, message);
			for(String erro : erros) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, erro, null));
			}
		}
	}


	public List<Erro> getListErro() {
		return listErro;
	}

	public void setListErro(List<Erro> listErro) {
		this.listErro = listErro;
	}

	public List<SelectItem> getListCLPSelectItem() {
		return listCLPSelectItem;
	}

	public void setListCLPSelectItem(List<SelectItem> listCLPSelectItem) {
		this.listCLPSelectItem = listCLPSelectItem;
	}

	public Long getIdCLPSelecionado() {
		return idCLPSelecionado;
	}

	public void setIdCLPSelecionado(Long idCLPSelecionado) {
		this.idCLPSelecionado = idCLPSelecionado;
	}



}
